//
//  FreeItemView.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/23.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "FreeItemView.h"

@implementation FreeItemView

- (void)setupSubViews
{
    self.icon = [[UIImageView alloc] initWithImage:PlaceHolderImage];
    [self addSubview:self.icon];
    self.icon.layer.cornerRadius = 4;
    self.icon.layer.masksToBounds = YES;
    
    self.itemRem = [[UILabel alloc] init];
    self.itemRem.numberOfLines = 0;
    self.itemRem.textColor = kColor333333;
    self.itemRem.font = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.itemRem];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kWidth(12));
        make.left.equalTo(self).offset(kWidth(15));
        make.width.equalTo(@(kWidth(115)));
        make.height.equalTo(@(75/115.0*kWidth(115)));
    }];

    [self.itemRem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.mas_right).offset(kWidth(9));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(15));
        make.top.equalTo(self.icon);
    }];
    
}

- (void)setRem:(NSString *)rem {
    
    self.itemRem.text = rem;
    NSDictionary *attr = @{NSFontAttributeName: self.itemRem.font};
    CGSize size = [rem boundingRectWithSize:CGSizeMake(kScreenW - kWidth(15) - kWidth(115) - kWidth(9) - kWidth(15), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kWidth(12));
        make.left.equalTo(self).offset(kWidth(15));
        make.width.equalTo(@(kWidth(115)));
        make.height.equalTo(@(75/115.0*kWidth(115)));
        if (size.height <= 75/115.0*kWidth(115)) {
            make.bottom.equalTo(self).offset(-kWidth(12));
        }
    }];
    
    [self.itemRem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.mas_right).offset(kWidth(9));
        make.right.lessThanOrEqualTo(self).offset(-kWidth(15));
        make.top.equalTo(self.icon);
        if (size.height > 75/115.0*kWidth(115)) {
            make.bottom.equalTo(self).offset(-kWidth(12));
        }
    }];
}

@end
